package com.itheima.controller.backend.security;

import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/setmeal")
@Slf4j
public class SetmealController {
    @Autowired
    private SetmealService setmealService;
    /**
     * 新增体检套餐
     */
    @PostMapping("/add")
    public Result add(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        log.info("新增体检套餐：{},{}",setmeal,checkGroupIds);
        setmealService.add(setmeal,checkGroupIds);
        return new Result(true,"添加体检套餐成功");
    }

    /**
     * 体检套餐分页查询
     */
    @PostMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        log.info("体检套餐分页查询");
        PageResult pageResult = setmealService.findPage(queryPageBean);
        return pageResult;
    }

    /**
     * 根据id查询
     */
    @GetMapping("/findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询体检套餐,{}",id);
        Setmeal setmeal = setmealService.findById(id);
        return new Result(true,"查询成功",setmeal);
    }

    //根据套餐id查询套餐id包含的多个检查组id
    @GetMapping("/findCheckgroupIdsBySetmealId")
    public Result findCheckgroupIdsBySetmealId(@RequestBody Integer id){
        try {
            List<Integer> checkGroupIds = setmealService.findCheckgroupIdsBySetmealId(id);
            return new Result(true,"查询成功",checkGroupIds);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,"查询失败");

        }}

    /**
     * 编辑体检套餐
     */
    @PostMapping("/edit")
    public Result edit(@RequestBody Setmeal setmeal,Integer[] checkgroupIds){
        log.info("新增体检套餐：{},{}",setmeal,checkgroupIds);
        setmealService.edit(setmeal,checkgroupIds);
        return new Result<>(true,"编辑体检套餐成功");
    }


}
